//      util.c
//      
//      Copyright 2011 DJ Shaji <djshaji@gmail.com>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.


#include "util.h"

void shell_log_handler ( gchar * log_domain, GLogLevelFlags log_level, 
						 const gchar * message, gpointer data )
{
	if ( log_level == G_LOG_LEVEL_DEBUG )
	{
		g_printerr ( "[debug]: %s\n", message ) ;
		return ;
	}
	
	GtkDialog * dialog = gtk_dialog_new () ;
	GtkHBox * box = gtk_hbox_new ( no, 2 ) ;
	
	gtk_box_pack_start ( dialog -> vbox, box, yes, yes, 5 ) ;
	
	GtkImage * image = gtk_image_new () ;
	gtk_box_pack_start ( box, image, no, no, 5 ) ;
	
	GtkLabel * label = gtk_label_new ( null ) ;
	gtk_label_set_line_wrap ( label, true ) ;
	
	gchar * markup = g_markup_printf_escaped ( "%s\n\n"
		"<span size=\"small\"><tt><b>April</b> Beta 1 </tt> Please tell Shaji "
		"about this bug / feature.</span>", message ) ;
	
	gtk_label_set_markup ( label, markup ) ;
	g_free ( markup ) ;
	
	gtk_box_pack_start ( box, label, yes, yes, 2 ) ;
	
	switch ( log_level )
	{
		case G_LOG_LEVEL_INFO:
		case G_LOG_LEVEL_MESSAGE:
			gtk_image_set_from_stock ( image, "gtk-dialog-info", GTK_ICON_SIZE_DIALOG ) ;
			break ;
		
		case G_LOG_LEVEL_DEBUG:
			gtk_image_set_from_stock ( image, "gtk-help", GTK_ICON_SIZE_DIALOG ) ;
			break ;
		case G_LOG_LEVEL_WARNING:
			gtk_image_set_from_stock ( image, "gtk-dialog-warning", GTK_ICON_SIZE_DIALOG ) ;
			break ;
		case G_LOG_LEVEL_CRITICAL:
		case G_LOG_LEVEL_ERROR:
			gtk_image_set_from_stock ( image, "gtk-dialog-error", GTK_ICON_SIZE_DIALOG ) ;
			break ;
		default:
			gtk_image_set_from_stock ( image, "gtk-dialog-info", GTK_ICON_SIZE_DIALOG ) ;
			break ;
	}
	
	gtk_dialog_add_button ( dialog, "Ok", -1 ) ;
	gtk_button_box_set_layout ( dialog -> action_area, GTK_BUTTONBOX_CENTER ) ;
	
	gtk_widget_show_all ( dialog ) ;
	gtk_dialog_run ( dialog ) ;
	
	gtk_widget_destroy ( dialog ) ;
}

Response prompt_question ( gchar * message )
{
	GtkDialog * dialog = gtk_message_dialog_new ( null,
									GTK_DIALOG_MODAL,
									GTK_MESSAGE_QUESTION,
									GTK_BUTTONS_NONE,
									"%s",
									message ) ;
	
	gtk_dialog_add_button ( dialog, "Yes", YES ) ;
	gtk_dialog_add_button ( dialog, "No", NO ) ;
	gtk_dialog_add_button ( dialog, "Cancel", CANCEL ) ;
	
	gtk_button_box_set_layout ( dialog -> action_area, GTK_BUTTONBOX_CENTER ) ;
	gtk_dialog_set_default_response ( dialog, CANCEL ) ;
	
	gtk_widget_show_all ( dialog ) ;
	Response r = gtk_dialog_run ( dialog ) ;
	
	gtk_widget_destroy ( dialog ) ;
	return r ;
}
									

void run_command ( gchar * command )
{
	GError * error = null ;
	
	gchar ** vector = g_strsplit ( command, ";", -1 ) ;
	g_spawn_async ( null, vector, null, G_SPAWN_DO_NOT_REAP_CHILD,
					null, null, null, & error ) ;
	
	if ( error )
		g_critical ( "Cannot run %s! %s", vector [0], error -> message ) ;
	
	g_strfreev ( vector ) ;	
}

void prompt_detailed_info ( gchar * title, gchar * message )
{
	GtkDialog * dialog = gtk_dialog_new_with_buttons ( "Properties",
														null,
														GTK_DIALOG_MODAL,
														"Ok",
														1,
														null ) ;
														
	GtkLabel * header = gtk_label_new ( null ) ;
	gchar * m = g_markup_printf_escaped ( "<big><b>%s</b></big>", title ) ;
	
	gtk_label_set_markup ( header, m ) ;
	g_free ( m ) ;
	
	gtk_box_pack_start ( dialog -> vbox, header, no, no, 5 ) ;
	gtk_label_set_line_wrap ( header, on ) ;
	
	GtkTextView * view = gtk_text_view_new () ;
	gtk_text_buffer_set_text ( gtk_text_view_get_buffer ( view ),
								message,
								strlen ( message )) ;
	
	GtkScrolledWindow * sw = gtk_scrolled_window_new ( null, null ) ;
	gtk_scrolled_window_set_policy ( sw, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ) ;
	
	gtk_box_pack_start ( dialog -> vbox, sw, yes, yes, 2 ) ;
	gtk_container_add ( sw, view ) ;
	
	gtk_button_box_set_layout ( dialog -> action_area, GTK_BUTTONBOX_CENTER ) ;
	gtk_window_resize ( dialog, 400, 300 ) ;
	
	gtk_widget_show_all ( dialog ) ;
	gtk_dialog_run ( dialog ) ;
	
	gtk_widget_destroy ( dialog ) ;
}

gchar * prompt_get_string ( gchar * title, gchar * message )
{
	GtkDialog * dialog = gtk_dialog_new_with_buttons ( title,
														null,
														GTK_DIALOG_MODAL,
														"Cancel",
														0,
														null ) ;
	
	GtkButton * ok = gtk_dialog_add_button ( dialog, "Ok", 1 ) ;
	GtkLabel * l = gtk_label_new ( message ) ;
	
	gtk_label_set_line_wrap ( l, on ) ;
	gtk_box_pack_start ( dialog -> vbox, l, no, no, 5 ) ;
	
	GtkEntry * entry = gtk_entry_new () ;
	gtk_box_pack_start ( dialog -> vbox, entry, no, no, 5 ) ;
	
	g_signal_connect_swapped ( entry, "activate", gtk_button_clicked, ok ) ;
	gtk_widget_show_all ( dialog ) ;
	
	gtk_button_box_set_layout ( dialog -> action_area, GTK_BUTTONBOX_CENTER ) ;
	
	if ( ! gtk_dialog_run ( dialog ))
	{
		gtk_widget_destroy ( dialog ) ;
		return null ;
	}
	
	gchar * s = null ;
	if ( gtk_entry_get_text_length ( entry ))
		s = g_strdup ( gtk_entry_get_text ( entry )) ;
		
	gtk_widget_destroy ( dialog ) ;
	return s ;
}
